package com.jiandan.load;

import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.jiandan.meizi.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class ListAdpter extends BaseAdapter {
    private static Context context;
    private List<String> url;
    private DisplayImageOptions options;
    private static int viewWidth;

    public ListAdpter(Context context, List<String> url) {
        this.context = context;
        this.url = url;
        options =
                new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty)
                        .showStubImage(R.drawable.ic_loading).showImageOnFail(R.drawable.ic_error)
                        .cacheInMemory().cacheOnDisc().displayer(new RoundedBitmapDisplayer(5))
                        .build();
        viewWidth = this.context.getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    public int getCount() {
        return url.size();
    }

    @Override
    public Object getItem(int position) {
        return url.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.meizi_listview_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.powerimage);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (holder.imageView != null)
            ImageLoader.getInstance().displayImage(url.get(position), holder.imageView, options,
                    new MyImageLoadingListener());
        return convertView;
    }

    static class MyImageLoadingListener extends SimpleImageLoadingListener {

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                int imageWidrh = loadedImage.getWidth();
                System.out.println("图片宽度====" + imageWidrh);
                int imageHeight = loadedImage.getHeight();
                System.out.println("图片高度====" + imageHeight);
                int viewHeight = (viewWidth * imageHeight) / imageWidrh;
                System.out.println("缩放后高度====" + viewHeight);

                view.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, viewHeight));
                ((ImageView) view).setImageBitmap(loadedImage);
            }else{
                ((ImageView) view).setImageBitmap(null);
            }
        }

    }

    static class ViewHolder {
        ImageView imageView;
    }
}
